package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    /**
     * 获取活动列表
     */
    @Override
    public PageResult<Activity> listActivity(Integer channel, Integer type, Integer status, Integer page, Integer pageSize) {
       //设置PageHelper参数
        PageHelper.startPage(page,pageSize);
       //调用mapper方法
        List<Activity> list = activityMapper.selectList(channel,type,status);

       //解析封装结果
        Page<Activity> p = (Page<Activity>)list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 添加活动
     */
    @Override
    public void add(Activity activity) {
        //补全基础属性createTime, updateTime
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    /**
     * 根据id查询活动
     */
    @Override
    public Activity getById(Integer id) {
        Activity activity = activityMapper.getById(id);
        return activity;
    }

    /**
     * 修改活动
     */
    @Override
    public void update(Activity activity) {
        //补全基础属性
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    /**
     * 删除活动
     */
    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }
    /**
     * 查询指定类型的活动
     */
    @Override
    public List<Activity> list(Integer type) {
        PageHelper.startPage(1,10);
        return activityMapper.list(type);
    }
}


